<?php
class JishiAction extends CommonAction {

	public function __construct(){
		parent::__construct();
		$this->checkAuth();
		$this->checkReal();
		
		$sys=M("Sys")->where("id=1")->find();
		if($sys['jishiopen']==0){
			$this->error("暂未开放");
		}

	}
	//空操作
	Public function _empty(){
	   header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
        $this->display("Public:404");
	}
    //市场
	public function index(){
	    $art=M('Art')->find(29);
	    $this->assign('art',$art);
	   $this->assign('xnb',$this->auth['xnb']);
	   $this->assign('dxnb',$this->auth['xnb_frozen']);
	    $this->assign('shouxufei',$this->sys['shichang_shouxufei']);//佣金
        $this->assign('biname',$this->sys['kgname']);//币种名称
        //记录 不显示自己
        $list=M('Jsorders')->where("status in (1,2)")->order("price asc")->select();
        foreach ($list as $k=>$v){
            $list[$k]['statusinfo']=$this->formartStatus($v['status']);
            $list[$k]['shouxufei']=$this->sys['shichang_shouxufei'];
            if (empty($v['chengjiao_time'])){
                $list[$k]['chengjiao_time']='--';
            }else {
	            $list[$k]['chengjiao_time']=date('Y-m-d H:i:s',$v['chengjiao_time']);
	        }
        }
        $this->assign('list',$list);
	    $this->display('./Tpl/Home/jishi_index.html');
	}
	public function sell(){
	    $num=!empty($_POST['num'])?$_POST['num']:null;
	    $price=!empty($_POST['price'])?$_POST['price']:null;
	    $password=!empty($_POST['password'])?$_POST['password']:null;
	    $shouxufei=$this->sys['shichang_shouxufei'];
	    if(empty($num)){
	        $data['info']='数量不能为空';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    if(empty($price)){
	        $data['info']='价格不能为空';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    if(empty($password)){
	        $data['info']='密码不能为空';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    if (md5($password)!=$this->auth['paypwd']){
	        $data['info']='交易密码不正确';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    if ($num>$this->auth['xnb']){
	        $data['info']='您的余额不足';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    $atr=array(
	        'uid'=>$this->auth['id'],
	        'num'=>$num,
	        'shengyu_num'=>$num,
	        'price'=>$price,
	        'deal'=>$num*$price*$shouxufei,
	        'type'=>1,
	        'status'=>1,
	        'ctime'=>time(),
	        'coin'=>'xnb'
	        
	    );
	    $r=M('Jsorders')->add($atr);
	    //扣钱
	    M('User')->where("id=".$this->auth['id'])->setDec('xnb',$num);
	    M('User')->where("id=".$this->auth['id'])->setInc('xnb_frozen',$num);
	    
	    if (!$r){
	        $data['info']='提交失败';
	        $data['status']=0;
	    }else {
	        $data['info']='提交成功';
	        $data['status']=1;
	    }
	    $this->ajaxReturn($data);
	}
	
	//买
	public function buy(){
	    $num=!empty($_POST['buy_num'])?$_POST['buy_num']:null;
	    $password=!empty($_POST['password'])?$_POST['password']:null;
	    $shouxufei=$this->sys['shichang_shouxufei'];
	    $id=intval($_POST['id']);
	     
	    if (md5($password)!=$this->auth['paypwd']){
	        $data['info']='交易密码不正确';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    if(empty($num)){
	        $data['info']='数量不能为空';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    $dan=M('Jsorders')->where("id=$id")->find();
	    if ($num>$dan['shengyu_num']){
	        $data['info']='购买数量超出了剩余数量';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    if ($num*$dan['price']>$this->auth['rmb']){
	        $data['info']='您的余额不足';
	        $data['status']=0;
	        $this->ajaxReturn($data);
	    }
	    //买家处理
	    M('User')->where("id={$dan['uid']}")->setDec('rmb',$num*$dan['price']);
	    M('User')->where("id={$dan['uid']}")->setInc('xnb',$num);
	    
	    //卖家
	    M('User')->where("id={$this->auth['id']}")->setInc('rmb',$num*$dan['price']*(1-$shouxufei));
	    M('User')->where("id={$this->auth['id']}")->setDec('xnb_frozen',$num);
	    
	    //订单    
	    M('Jsorders')->where("id=$id")->setDec('shengyu_num',$num);
	    M('Jsorders')->where("id=$id")->setField('chengjiao_time',time());
	    //部分购买
	    if ($dan['num']==$dan['shengyu_num']){
	        M('Jsorders')->where("id=$id")->setField('status', 2);
	    }
	    //购买完毕
	    if ($dan['shengyu_num']<=$num){
	        M('Jsorders')->where("id=$id")->setField('status', 3);
	    }
	    //写入日志
	    $log=array(
	        'num'=>$num,
	        'price'=>$dan['price'],
	        'deal'=>$num*$dan['price']*$shouxufei,
	        'add_time'=>time(),
	        'status'=>1,
	        'uid'=>$dan['uid'],
	        'ctime'=>$dan['ctime'],
	        'coin'=>'xnb'
	    );
	    M('Jsorders_log')->add($log);
	    //奖励
	    $jiangli_one=$num*$dan['price']*$shouxufei*$this->sys['jiangli_one'];
	    $jiangli_two=$num*$dan['price']*$shouxufei*$this->sys['jiangli_two'];
	    //一级上线
	    $one=M('User')->where("username='{$this->auth['invitup']}'")->find();
        if (!empty($one)){
              //奖励
              M('User')->where("username='{$one['username']}'")->setInc("rmb",$jiangli_one);
              $data=array();
              $data=array(
                  'user'=>$_SESSION['USER_KEY_ID'],
                  'upuser'=>$one['id'],
                  'money'=>$jiangli_one,
                  'status'=>1,
                  'time'=>time(),
                  'type'=>4
              );
              M('Factory_jiangli')->add($data);
              //二级
              $two=M('User')->where("username='{$one['invitup']}'")->find();              
              if (!empty($two)){
                  M('User')->where("username='{$two['username']}'")->setInc("rmb",$jiangli_two);
                  $data=array();
                  $data=array(
                      'user'=>$_SESSION['USER_KEY_ID'],
                      'upuser'=>$two['id'],
                      'money'=>$jiangli_two,
                      'status'=>1,
                      'time'=>time(),
                      'type'=>4
                  );
                  M('Factory_jiangli')->add($data);
              }
        }	    
	    $data['info']='买入成功';
	    $data['status']=1;
	    $this->ajaxReturn($data);
	}
	//交易
	public function jiaoyi(){
	    //记录
	    $list=M('Jsorders')->where("status in (1,2) and uid !={$_SESSION['USER_KEY_ID']}")->order("price asc")->select();
	    foreach ($list as $k=>$v){
	        $list[$k]['statusinfo']=$this->formartStatus($v['status']);
	        $list[$k]['shouxufei']=$this->sys['shichang_shouxufei'];
	        if (empty($v['chengjiao_time'])){
	            $list[$k]['chengjiao_time']='--';
	        }else {
	            $list[$k]['chengjiao_time']=date('Y-m-d H:i:s',$v['chengjiao_time']);
	        }
	    }
	    $this->assign('list',$list);
	    $this->display('./Tpl/Home/jishi_jiaoyi.html');
	}
	//列表
    public function buylist(){
        import('ORG.Util.Page');// 导入分页类
        $count      = M('Jsorders_log')->count();// 查询满足要求的总记录数
        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $list=M('Jsorders_log')->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('name',$this->sys['kgname']);
        $this->display('./Tpl/Home/jishi_buylist.html');
    }
	private function formartStatus($status){
	    switch($status){
	        case 0:$statusinfo='交易已取消';break;
	        case 1:$statusinfo='等待交易';break;
	        case 2:$statusinfo='部分成交';break;
	        case 3:$statusinfo='交易完成';break;
	    }
	    return $statusinfo;
	}
}